package com.bocai.duobao.api;

import com.bocai.Response;
import com.bocai.ResponseCode;
import com.bocai.duobao.model.entity.DbBetDetail;
import com.bocai.duobao.model.param.BetParam;
import com.bocai.duobao.service.DuobaoService;
import com.bocai.aspect.LoginChecker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.IOException;
import java.util.stream.Collectors;

/**
 * @author create by VIC.Huang
 * @date 2018/1/16
 */
@RestController()
@RequestMapping("/duobao")
public class DuobaoApi {
    @Autowired
    private DuobaoService duobaoService;

    /**
     * 获取夺宝玩法列表
     *
     * @throws IOException
     */
    @GetMapping("/list")
    public Response duobaoList() {
        return new Response(duobaoService.getDuobaoList());
    }

    /**
     * 查看夺宝详情
     *
     * @param gameId
     * @throws IOException
     */
    @GetMapping("/detail/{gameId}")
    public Response duobaoDetail(@RequestHeader(value = "token", required = false, defaultValue = "") String token,
                                 @PathVariable("gameId") Long gameId) {
        return new Response(duobaoService.getDuobaoDetail(gameId, token));
    }

    /**
     * 下注
     */
    @PostMapping("/bet")
    @LoginChecker
    public Response bet(@RequestHeader(value = "token", required = false, defaultValue = "") String token,
                        @Valid @RequestBody BetParam betParam, Errors errors) {
        if (errors.hasErrors()) {
            return new Response(ResponseCode.REQUEST_PARAM_ERROR.getValue(),
                    errors.getAllErrors().stream().map(er -> er.getDefaultMessage()).collect(Collectors.joining(";"))
            );
        }

        try {
            DbBetDetail betDetail = new DbBetDetail();
            betDetail.setDbGameId(betParam.getGameId())
                    .setBetNum(betParam.getBetNum())
                    .setAmount(betParam.getAmount())
                    .setIssueNo(betParam.getIssueNo());
            duobaoService.bet(betDetail, token);
            return new Response(ResponseCode.SUCCESS.getValue(), "下注成功");
        } catch (Exception e) {
            return new Response(ResponseCode.SERVER_ERROR.getValue(), e.getMessage());
        }
    }

    /**
     * 获取往期开奖记录
     */
    @GetMapping("/openRecords")
    public Response openRecords(@RequestParam(value = "page", required = false, defaultValue = "1")Integer page) {
        return new Response(duobaoService.getOpenRecords(page));
    }

    /**
     * 下次开奖时间
     * @return
     */
    @GetMapping("/nextOpenTime")
    public Response nextOpenTime() {
        return new Response(duobaoService.getLeftSecond());
    }

    /**
     * 获取最新参与记录； 抢购榜单
     *
     * @param gameId 有gameId参数时，获取该玩法的名单；没有时获取所有
     */
    @GetMapping("/latestBetRecords")
    public Response latestBetRecords(@RequestParam(value = "gameId", required = false, defaultValue = "0") Long gameId,
                                     @RequestParam(value = "page", required = false, defaultValue = "1")Integer page) {
        return new Response(duobaoService.getLatestBetRecords(gameId, page));
    }

    /**
     * 获取最新获奖名单
     */
    @GetMapping("/latestWinRecords")
    public Response lastestWinRecrods(@RequestParam(value = "page", required = false, defaultValue = "1")Integer page) {
        return new Response(duobaoService.getLatestWinRecords(page));
    }
}
